package cn.e4j.modular.system.service.impl;

import cn.e4j.core.datascope.DataScope;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.system.entity.SysUserRole;
import cn.e4j.modular.system.mapper.SysUserRoleMapper;
import cn.e4j.modular.system.service.SysUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户-角色关联表 服务实现类
 * </p>
 *
 * @author wenchuan.li
 * @since 2018-12-06
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, String>> selectRoleidsByUser(String Userid) {
        return this.baseMapper.selectRoleidsByUser(Userid);
    }

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, String>> selectUseridsByRole(String Roleid) {
        return this.baseMapper.selectUseridsByRole(Roleid);
    }

    @Transactional
    @Override
    public void addUserRoles(List<SysUserRole> lists) {
        for (SysUserRole sysUserRole : lists) {
            this.baseMapper.insert(sysUserRole);
        }
    }

    @Transactional
    @Override
    public void delUserRoles(List<SysUserRole> lists) {
        for (SysUserRole sysUserRole : lists) {
            if (ToolUtil.isNotEmpty(sysUserRole.getUSER_ID()) && ToolUtil.isNotEmpty(sysUserRole.getROLE_ID())) {
                Map<String, Object> map = new HashedMap();
                map.put("USER_ID", sysUserRole.getUSER_ID());
                map.put("ROLE_ID", sysUserRole.getROLE_ID());
                this.baseMapper.deleteByMap(map);
            }
        }
    }

}
